﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

namespace DemoWebApp
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        [WebMethod]
        public string[] GetWordSuggestions(string prefixText, int count, string contextKey)
        {
            //Set your Datasource for words that will suggested.
            //Manually entered here as sample, make a query to your database and get compitable words.

            var suggestions = (from w in StaticDatasource.MyDatasource
                               where w.Text.ToLower().Contains(prefixText.ToLower())
                               select new
                               {
                                   w.WordId,
                                   w.Text
                               });

            List<string> myList = new List<string>();
            foreach (var item in suggestions)
            {
                myList.Add(AjaxControlToolkit.AutoCompleteExtender
                    .CreateAutoCompleteItem(item.Text, item.WordId.ToString()));
            }

            return myList.Skip(0).Take(count).ToArray();
        }
    }
}
